FROM node:18

# Add build arguments for proxy
# ARG HTTP_PROXY
# ARG HTTPS_PROXY
ENV http_proxy=http://10.237.14.172:1080
ENV https_proxy=http://10.237.14.172:1080
# Install dependencies for Puppeteer
RUN apt-get update && apt-get install -y \
    libglib2.0-0 \
    libnss3 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libpango-1.0-0 \
    libcairo2 \
    libasound2 \
    && rm -rf /var/lib/apt/lists/*

# Configure npm to use proxy
RUN if [ ! -z "$HTTP_PROXY" ]; then \
        npm config set proxy $HTTP_PROXY \
        && npm config set https-proxy $HTTPS_PROXY; \
    fi

WORKDIR /app

# Install TypeScript globally
RUN npm install -g typescript

# Copy package files
COPY package*.json ./

# Clean npm cache and remove existing node_modules
RUN npm cache clean --force && \
    rm -rf node_modules && \
    rm -rf /root/.npm/_cacache

# Install dependencies with force flag
RUN npm install --force

# Copy source code
COPY . .

# Build TypeScript code
RUN tsc

# Expose port
EXPOSE 8080

# Start the application
CMD ["npm", "start"]